package short_semester.questionnaire_planet.service.analyse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import short_semester.questionnaire_planet.dao.*;
import short_semester.questionnaire_planet.entity.User;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.response.fillInResponse.FillInResponse;
import short_semester.questionnaire_planet.response.fillResponse.OptionRecordResponse;
import short_semester.questionnaire_planet.response.fillResponse.QuestionRecordResponse;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class SingleChoiceQuestionRecordService {

    @Autowired
    SingleChoiceQuestionRecordDao singleChoiceQuestionRecordDao;

    @Autowired
    QuestionnaireDao questionnaireDao;

    public List<FillInResponse> getResponse(
            QuestionnaireRecord questionnaireRecord, Integer questionnaireId) {
        List<FillInResponse> result = new ArrayList<>();
        Questionnaire questionnaire = questionnaireDao.findById(questionnaireId).get();
        List<SingleChoiceQuestion> list = singleChoiceQuestionDao.findByQuestionnaire(questionnaire);
        for (SingleChoiceQuestion eachQuestion: list) {
            Optional<SingleChoiceQuestionRecord> optional = singleChoiceQuestionRecordDao
                    .findByQuestionnaireRecordAndSingleChoiceQuestion(questionnaireRecord, eachQuestion);
            if (!optional.isPresent()) {
                FillInResponse response = new FillInResponse();
                response.setType("radio");
                response.setIsAnswer(false);
                response.setId(eachQuestion.getId());
                result.add(response);
            }
            else {
                FillInResponse response = new FillInResponse();
                SingleChoiceQuestionRecord record = optional.get();
                response.setType("radio");
                response.setIsAnswer(true);
                response.setOptionId(record.getAnswer());
                response.setId(eachQuestion.getId());
                result.add(response);
            }
        }
        return result;
    }

    @Autowired
    SingleChoiceQuestionDao singleChoiceQuestionDao;

    @Autowired
    UserDao userDao;

    @Autowired
    SingleChoiceOptionDao singleChoiceOptionDao;

    public void addRecord(
            Integer questionId,
            QuestionnaireRecord questionnaireRecord,
            Integer optionId
    ) {
        
        Optional<SingleChoiceQuestion> singleChoiceQuestion = singleChoiceQuestionDao.findById(questionId);
        if (!singleChoiceQuestion.isPresent())
            return;
        SingleChoiceQuestion question = singleChoiceQuestion.get();
        
        Optional<SingleChoiceQuestionRecord> ret = singleChoiceQuestionRecordDao
                .findByQuestionnaireRecordAndSingleChoiceQuestion(questionnaireRecord, question);
        SingleChoiceQuestionRecord record;
        if (ret.isPresent()) {
            record = ret.get();
        }
        else {
            record = new SingleChoiceQuestionRecord();
            SingleChoiceOption option = singleChoiceOptionDao.getById(optionId);

            record.setSingleChoiceQuestion(question);
            singleChoiceQuestionDao.save(question);
            record.setQuestionnaireRecord(questionnaireRecord);
        }

        record.setAnswer(optionId);
        singleChoiceQuestionRecordDao.save(record);
    }
    
    public void submitRecord(QuestionnaireRecord questionnaireRecord){
        for (SingleChoiceQuestionRecord questionRecord : singleChoiceQuestionRecordDao
                .findAllByQuestionnaireRecord(questionnaireRecord)) {
            if(questionRecord.getAnswer()!=null){
                singleChoiceQuestionDao.submitQuestion(questionRecord.getSingleChoiceQuestion().getId());
                singleChoiceOptionDao.submitOption(questionRecord.getAnswer());
            }
        }
    }
    
    //获取题目记录信息
    public QuestionRecordResponse getRecord(QuestionnaireRecord record,Integer questionId){
        //题目基本信息
        SingleChoiceQuestion question = singleChoiceQuestionDao.getById(questionId);
        SingleChoiceQuestionRecord questionRecord = singleChoiceQuestionRecordDao.findByQuestionnaireRecordAndSingleChoiceQuestion(record, question).get();
        QuestionRecordResponse questionRecordResponse = new QuestionRecordResponse(question,questionRecord);
        
        //选项信息
        for (Integer optionId : questionRecord.getOptionSequence()) {
            SingleChoiceOption option = singleChoiceOptionDao.getById(optionId);
            OptionRecordResponse optionRecordResponse = new OptionRecordResponse(option);
            questionRecordResponse.getOptions().add(optionRecordResponse);
        }
        return  questionRecordResponse;
    }
    
    //计算分数
    public int compute(QuestionnaireRecord record ,Integer questionId){
        int grade = 0;
        SingleChoiceQuestion question = singleChoiceQuestionDao.getById(questionId);
        SingleChoiceQuestionRecord questionRecord = singleChoiceQuestionRecordDao.findByQuestionnaireRecordAndSingleChoiceQuestion(record, question).get();
        Integer answerId = questionRecord.getAnswer();
        for (SingleChoiceOption option : singleChoiceOptionDao.findBySingleChoiceQuestion(question)) {
            if(option.getIsCorrect()&&Objects.equals(option.getId(), answerId)){
                grade+=question.getGrade();
                break;
            }
        }
        
        return grade;
    }
}
